<?php
class CP_ProductsController extends XPHP_Controller
{
	/**
	 * Phương thức sẽ thực thi trước khi thực thi sau khi controller được khởi tạo
	 */
	public function _init()
	{
		//Load layout
		$this->loadLayout("Administrator");
		//Xác thực
		$this->helper->auth();
	}
	
	public function indexAction()
	{
		$modelProducts = new Areas_CP_Models_Products();
		$arrProducts = $modelProducts->select_all(false, "date_create DESC");
		//Lấy tên danh mục sản phẩm
		foreach ($arrProducts as $id => $product)
		{
			$cat = new Areas_CP_Models_ProductCategories($product["categoryID"]);
			$arrProducts[$id]["categoryName"] = $cat->name;
		}
		//Trả về view
		$this->view->arrProducts = $arrProducts;

		return $this->view();
	}
	
	/**
	 * [[
	 * model: Areas_CP_Models_Products
	 * ]]
	 */
	public function createAction()
	{
		$modelCategories = new Areas_CP_Models_ProductCategories();
		$arrCategories = $modelCategories->select_all();
		
		//Sắp xếp danh mục cha con
		$arrData = array();
		foreach ($arrCategories as $key => $item)
		{
			if ($item['parentID'] == 0)
			{
				$arrData[$key] = $item;
				foreach ($arrCategories as $key2 => $item2) {
					if ($item2['parentID'] == $item['id'])
						$arrData[$key2] = $item2;
				}
			}
		}
		//Lấy danh sách kích cỡ
		$modelSizes = new Areas_CP_Models_Sizes();
		$arrSizes = $modelSizes->select_all();
		//Danh mục chất liệu sản phẩm
		$modelMaterials = new Areas_CP_Models_Materials();
		$arrMaterials = $modelMaterials->select_all(); 
		//Lấy danh sách nhãn hiệu
		$modelBrands = new Areas_CP_Models_Brands();
		$arrBrands = $modelBrands->select_all();
		$this->view->arrBrands = $arrBrands;
		$this->view->arrsizes = $arrSizes;
		$this->view->arrMaterials = $arrMaterials;
		$this->view->arrCategories = $arrData;

		return $this->view();
	}
	
	/**
	 * [[
	 * model: Areas_CP_Models_Products
	 * ]]
	 */
	public function createPOST()
	{
		if ($this->hasModel())
		{
			if ($this->model->isValid())
			{
				//Kiểm tra mã sản phẩm đã tồn tại hay chưa
				$modelProducts = new Areas_CP_Models_Products();
				$data = $modelProducts->select("id='" . $this->model->id . "'");
				if (empty($data))
				{
					if ($this->model->highlight)
						$this->model->highlight = 1;
					
					$size = "";
					foreach ($_POST['size'] as $s)
					{
						if ($size == "")
							$size .= $s;
						else 
							$size .= "," . $s;
					}
					
					$this->model->size = $size;
					
					$this->model->date_create = XPHP_Date::toTime(date("H:i:s d/m/y"));
					$this->model->insert();
					//Trả về RedirectResult tới action index
					return $this->redirect("index");
				}
				//Nếu tồn tạo mã
				else
					$this->model->setErrorMessage("unique", "Mã sản phẩm này đã tồn tại", "id");
			}
		}
	}

	public function editAction($args)
	{
		$modelCategories = new Areas_CP_Models_ProductCategories();
		$arrCategories = $modelCategories->select_all();
		
		//Sắp xếp danh mục cha con
		$arrData = array();
		foreach ($arrCategories as $key => $item)
		{
			if ($item['parentID'] == 0)
			{
				$arrData[$key] = $item;
				foreach ($arrCategories as $key2 => $item2) {
					if ($item2['parentID'] == $item['id'])
						$arrData[$key2] = $item2;
				}
			}
		}
		
		$model = new Areas_CP_Models_Products($args[0]);
		//Lấy danh sách kích cỡ
		$modelSizes = new Areas_CP_Models_Sizes();
		$arrSizes = $modelSizes->select_all();
		//Xử lý hiển thị size
		$arrCZ = $model->size;
		$arrCZ = explode(",", $arrCZ);
		$html = "";
		foreach ($arrSizes as $size)
		{
			$html .= '<option value=' . $size['id'];
			foreach ($arrCZ as $cz)
			{
				if ($size['id'] == $cz)
				{
					$html .= ' class="selected"';
					break 1;
				}
			}
			$html .= '>' . $size['name'] . '</option>';
		}
		
		//Danh mục chất liệu sản phẩm
		$modelMaterials = new Areas_CP_Models_Materials();
		$arrMaterials = $modelMaterials->select_all(); 
		//Lấy danh sách nhãn hiệu
		$modelBrands = new Areas_CP_Models_Brands();
		$arrBrands = $modelBrands->select_all();
		$this->view->arrBrands = $arrBrands;
		$this->view->options = $html;
		$this->view->arrMaterials = $arrMaterials;
		$this->view->arrCategories = $arrData;
		
		return $this->view($model);
	}
	
	/**
	 * [[
	 * model: Areas_CP_Models_Products
	 * ]]
	 */
	public function editPOST()
	{
		if ($this->hasModel())
		{
			if ($this->model->isValid())
			{
				if ($this->model->highlight)
					$this->model->highlight = 1;
				$size = "";
					foreach ($_POST['size'] as $s)
					{
						if ($size == "")
							$size .= $s;
						else 
							$size .= "," . $s;
					}
					
					$this->model->size = $size;
				$this->model->update();
				//Trả về RedirectResult tới action index
				return $this->redirect("index");
			}
		}
	}
	
	public function detailAction($args)
	{
		$model = new Areas_CP_Models_Products($args[0]);
		//Chuyển các mã thành tên
		//Danh mục
		$cat = new Areas_CP_Models_ProductCategories($model->categoryID);
		$model->categoryID = $cat->name;
		//Nhãn hiệu
		$brand = new Areas_CP_Models_Brands($model->brandID);
		$model->brandID = $brand->name;
		//Chất liệu
		$meter = new Areas_CP_Models_Materials($model->materialID);
		$model->materialID = $meter->name;
		//Kích cỡ
		$size = new Areas_CP_Models_Sizes($model->sizeID);
		$model->sizeID = $size->name;
		
		return $this->view($model);
	}
	
	public function deleteAJAX($args)
	{
		$id = $this->params['id'];
		$model = new Areas_CP_Models_Products($id);
		$model->delete();
		//Trả về kết quả
		return $this->json(array("success" => true));
	}
}